EXAMPLE SOURCE CODES


     This is a small collection of mostly game example source codes. These source codes are made available to help PlayBasic programmers kick start their game programming journey. Looking for more source code / tutorials & media, then remember to visit the PlayBasic Resource board on our forums.

Found #5 items in Maps category by Kevin Picone

 Make Occupied Map From 2 Colour image

By: Kevin Picone Added: August 23rd, 2013

Category: All,Maps


This example computes map of 'occupied' rects from the 2 colour image. The Image is expected to be either black (rgb=0 ) or pure white (rgb=$ffffff). The user can draw circles with the mouse to fill the frame in. The occupied Map is drawn behind it in GREEN and RED blocks in real time.


Download: Login to Download




 Selective Tile Map Refreshing II

By: Kevin Picone Added: May 20th, 2012

Category: All,Maps


    This is a more updated version of the previous example. The only real difference is that, this one uses built maps where is the original one demonstrates the hands on method. Apart from that they're pretty much the same, with just some tweaks for newer editions of PlayBasic V1.64M and above.

    This is another short PlayBasic source code example, this one's demonstrating a selective redrawing method using PlayBasic Maps. The program attempts to reduce the cost per refresh by not redrawing the entire backdrop picture every frame. It does this by tagging the overdrawn zones that need to be restored using a Map. So basically the backdrop picture is first cut up into an Map representation. In this example the block size is 32x by 32y pixels, giving us a level size of (800/32 by 600/32). The demo uses two versions of the level tile map data. One is the original and the second is a version we're going to draw from. We need two since the redraw version gets cleared and altered each frame.

     When drawing character objects (in this demo those are the circular blobs), we move the object then convert it's 2d space coordinates back into map coordinates. Then copy from this tile/block area from Original Level to the Refresh Level. We repeat this process for all on screen objects. So after processing our characters, the refresh map contains the parts of the backdrop that need to refreshed (reset) back to it's original state before drawing the next frame. So rather than redraw the entire backdrop picture every time, we can selectively refresh the backdrop. This type of method works really well in 2D games, as it allows the program to lower the cost per-pixel, ironing out the performance for older systems. By older, I mean systems beyond 5 years, closer to 10 years old really

Download: Login to Download




 4 Way Map Movement (Moving through tunnels)

By: Kevin Picone Added: May 28th, 2011

Category: All,Maps,Collision


     This example lets the user navigate an object through a map. The object is only allowed to move in 4 directions and the object is assumed to be the same size as a map tile.

     The path ways through the map must also be a single block in size, so the object fits tightly. Normally this would mean moving through the tunnel would require pixel perfect navigation. To counter this, the movement code takes two approaches. Firstly, there's check to see if the object is able to move in the requested direction. So when the player presses up, we check the tile above, if that's empty they're allowed to move up. If that fails. We fall into a second phase, where we look at the tiles above and the left/right of our current position. If one of those is empty, then we then translate the user up request, into either a left or right movement. Which will slide them into position.

     Code built with PB1.64N beta 4 (may need changes for older versions)


Download: Login to Download




 Selective (Tile Based) Refreshing

By: Kevin Picone Added: October 1st, 2008

Category: All, Fps, Tutorial,Maps


This is a quick example of a dirty rectangle style refresh using a tile map. The example allows you to render a 'static' backdrop page with a bunch of the sprites moving over it. The primary objective of such approaches is to reduce the cost per pixel.
Download: Login to Download




 Convert Map To Collision Worlds

By: Kevin Picone Added: August 31st, 2005

Category: All,Maps,Collision,Worlds

     This code creates a game level with a series of randomly colored boxes on the screen, which are drawn as an image. The image is then imported into a map called "MyMap." The code then reads in a level from data statements, makes the level transparent, and draws it to the screen. The code also creates a collision world from the map, and sets up a sprite and camera.

    The sprite can be moved around the screen using arrow keys, and will automatically slide off walls it hits in the collision world. The camera is positioned over the sprite, and will draw what is in view. The game will continue to loop until the space key is pressed.

PlayBasic Code:
  MyMap =NewMap(1)  
  
;  
  TileWidth=48
  TileHeight=48
  Number_of_tiles = 5
  
; Loop through and draw a series of randomly coloured Boxes to the screen
  For lp=1 To Number_of_tiles
     xpos=lp*tileWidth  
     BoxC Xpos,0,Xpos+TileWidth,TileHeight,1,RndRGB()     
  Next 

; Grab the box graphics that we've just drawn as image.
  TempImage=GetFreeImage()
  GetImage TempImage,0,0,xpos+TIleWidth,Tileheight
  
  
; Import this image into our previously defined Map.  
  MakeMapGFX MyMAP,TempImage,TileWidth,TileHeight,Number_Of_Tiles,RGB(0,0,0)



; =======================================================
; Read the Level in from the data statements bellow
; =======================================================
  
   Read_Level(1,1)

   Leveltransparent 1,1,0
    

; Draw the Level to the screen at Xpos 0 and Ypos 0
   Xpos=0
   Ypos=0


 ; Create the Collision world
   MyCollisionWorld=NewWorld()
   CaptureToWorld MyCollisionWorld
   MakeCollisionWorldFromMap(MyMap,1,xpos,ypos)
   PartitionWorld MyCollisionWorld,64

; restore drawing back to immediate mode
   drawgfximmediate
   
   
   PLayerSize = 32
   
; Create an image of a circle (for something to look at)
   Cls 0
   Size=PLayerSize/2
   Circlec Size,Size,Size,1,$00ff00
   GetImage 10,0,0,PLayerSize,PLayerSize
   preparefximage 10


; create a sprite 
   CreateSprite 1
   autocenterspritehandle 1,true
   SpriteImage 1,10
   spriteCollision 1,true
;  Set collision mode to World
   SpriteCollisionMode 1,4
   SpriteCollisionWorld 1,MyCollisionWorld

; Set the Sprites Collision Circle to just smaller than then players
; image.  In this example the players image is also a circle.
   SpriteCollisionRadius 1,(PlayerSize/2)-2
;   SpriteCollisionDebug 1,true
   
   PositionSprite 1,150,150

 ; Create a Camera
   CreateCamera 1 
  

   Do
        CaptureToScene
        ClsScene 
     
        Capturedepth 100          
        DrawMap MyMap,1,0,0

   
    ; MOve the Sprite around (8 way movement)
        Speed=5
     
        Xspeed=0
        Yspeed=0
        if LeftKey() then Xspeed=-Speed
        if RightKey() then Xspeed=Speed
        if UpKey() then Yspeed=-Speed
        if DownKey() then Yspeed=Speed
   
        if Xspeed<>0 or Yspeed<>0 
        ; MOve the sprite, this will auto slide the sprite
        ; off any wall it hits in the Collision World 
                MoveSprite 1,xspeed,yspeed
        endif
     
        DrawAllSprites

    ;POsition the camera over the sprite
        Xpos=GetSpriteX(1)-(GetScreenWidth()/2)
        Ypos=GetSpriteY(1)-(GetScreenHeight()/2)
        POsitionCamera 1,Xpos,Ypos

    ; Tell the camera to draw whats in viwe
        Drawcamera 1     

    	 Sync
    	 
   loop spacekey()
end





; =============================================================================
;  This Function converts a map Level into collision world, which is nothing
; more than drawing lines around the bounding area around of the tiles.  It
; considers tile zero to be transparent (which is soft). Everything else is
; think is hard  
;
;  once the map is a collision world you can run ray interest on it, sliding
; collision among other things..
; =============================================================================


   Acreset
   Constant EdgeState_Searching   =ac(1)
   Constant EdgeState_FoundStart   =ac(1)


Function MakeCollisionWorldFromMap(thisMap,Thislevel,xpos,ypos)

   LevelWidth   =GetLevelWidth(Thismap,thislevel)
   LevelHeight =GetLevelHeight(Thismap,thislevel)

   BlockWidth=GetMapBLockWidth(ThisMap)
   BlockHeight=GetMapBLockHeight(ThisMap)
  
 ; ====================================================================
; #1 first Pass over the level looking for blocks running Left to Right
 ; =====================================================================

   Dot 0,0

   Y=Ypos
   For Ylp=0 to LevelHeight
  Y2=Y+BlockHeight
  X=Xpos
  
  TopEdgeState=EdgeSTate_Searching
  TopStartXpos=Xpos

  BotEdgeState=EdgeSTate_Searching
  BotStartXpos=Xpos
  
  For Xlp=0 to LevelWidth

     CurrentTile   =PeekLevelTile(ThisMap,Thislevel,Xlp,Ylp)

 ; Check if this tile is not our transparent tile zero  
     If CurrentTile<>0

  ; Check the Y position to make sure we don't step off the Level array
    iF Ylp>0
   ; Grab the tile above our current tile
       AboveCurrentTile  =PeekLevelTile(Thismap,Thislevel,Xlp,Ylp-1)
    
   ; Check if this tile is transparent
       if AboveCurrentTile=0  
    ; Since there's no tile above then this must be a top
    ; edge of the block
      if TopEdgeState=EdgeSTate_Searching
         inc TopEdgeState              
         TopStartXpos=X      
      endif
       else
      if TopEdgeState=EdgeSTate_FoundStart
        TopEdgeState=EdgeSTate_Searching
 ;'       Line TopStartXpos,y,X,y
        Line x,y,TopStartXpos,y
      endif
       endif      
    Endif

  ; Check the Y position to make sure we don't step off the Level array
    if Ylp<LevelHeight
   ; Grab the tile above our current tile
       BellowCurrentTile  =PeekLevelTile(Thismap,Thislevel,Xlp,Ylp+1)

   ; Check if this tile is transparent
       if BellowCurrentTile=0  
    ; Since there's no tile above then this must be a top
    ; edge of the block
      if BotEdgeState=EdgeSTate_Searching
         inc BotEdgeState              
         BotStartXpos=X      
      endif
       else
      if BotEdgeState=EdgeSTate_FoundStart
        BotEdgeState=EdgeSTate_Searching
        Line BotStartXpos,y2,X,y2
      endif
       endif      
    endif

     else
  ; close edges
    if TopEdgeState=EdgeSTate_FoundStart
       TopEdgeState=EdgeSTate_Searching
       Line x,y,TopStartXpos,y
    endif
    if BotEdgeState=EdgeSTate_FoundStart
       BotEdgeState=EdgeSTate_Searching
       Line BotStartXpos,y2,X,y2
    endif
     endif

     X=X+BlockWidth
  next
  Y=Y+BlockHeight
   next


 ; ====================================================================
; #2 now we repeat this process, but scanning from Top to Bottom
; =====================================================================

   X=Xpos
   For Xlp=0 to LevelWidth
  X2=X+BlockWidth
  Y=Ypos
  
  LeftEdgeState=EdgeSTate_Searching
  LeftStartYpos=Y

  RightEdgeState=EdgeSTate_Searching
  RightStartYpos=Y
  
  For ylp=0 to LevelHeight
  
     CurrentTile   =PeekLevelTile(ThisMap,Thislevel,Xlp,Ylp)

 ; Check if this tile is not our transparent tile zero  
     If CurrentTile<>0

  ; Check the Y position to make sure we don't step off the Level array
    iF Xlp>0
       
   ; Grab the tile to the LEFT of our current tile
       PreviousTile  =PeekLevelTile(Thismap,Thislevel,Xlp-1,Ylp)
    
   ; Check if this tile is transparent
       if PreviousTile=0  
    ; Since there's no tile then this must be the LEFT
    ; edge of the block
      if LeftEdgeState=EdgeSTate_Searching
         inc LeftEdgeState              
         LeftStartYpos=y      
      endif
       else
      if LeftEdgeState=EdgeSTate_FoundStart
        LeftEdgeState=EdgeSTate_Searching
        Line x,LeftStartYpos,x,y
      endif
       endif      
    Endif

  ; Check the X position to make sure we don't step off the Level array
    if Xlp<LevelWidth

   ; Grab the tile above our current tile
       NextTile  =PeekLevelTile(Thismap,Thislevel,Xlp+1,Ylp)

   ; Check if this tile is transparent
       if NextTile=0  
    ; Since there's no tile on the RIGHT then this must be a right
    ; edge of the block
      if RightEdgeState=EdgeSTate_Searching
         inc RightEdgeState              
         RightStartYpos=Y      
      endif
       else
      if RightEdgeState=EdgeSTate_FoundStart
        RightEdgeState=EdgeSTate_Searching
;        Line x2,RightStartYpos,x2,y
        Line x2,y,x2,RightStartYpos
      endif
       endif      
    endif

     else

  ; close edges (if open)
    if LeftEdgeState=EdgeSTate_FoundStart
       LeftEdgeState=EdgeSTate_Searching
       Line x,LeftStartYpos,x,y
    endif

    if RightEdgeState=EdgeSTate_FoundStart
      RightEdgeState=EdgeSTate_Searching
;'      Line x2,RightStartYpos,x2,y
      Line x2,y,x2,RightStartYpos
    endif

     endif

     Y=Y+BlockHeight
  next
  X=X+BlockWidth
   next


EndFunction



` *=----------------------------------------------------------------=*
`                    -------- Read Map ---------
` *=----------------------------------------------------------------=*

Function Read_Level(ThisMap,ThisLevel)
   Map_Width=readdata()
   Map_Height=readdata()

; Init the Level array within this Map
   createlevel ThisMap,ThisLevel,Map_Width+1,Map_Height+1

   for zlp=0 to map_Height-1
  for xlp=0 to map_width-1
     pokeleveltile ThisMap,Thislevel,xlp,zlp,ReadData()
  next xlp
   next zlp

EndFunction Map_Width,Map_Depth




 data 32
 data 42
 data 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
 data 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1
 data 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1
 data 1,0,0,0,0,2,2,2,2,0,2,0,0,0,1,0,1,0,1,0,1,0,0,0,0,0,0,0,0,0,0,1
 data 1,0,0,0,0,2,0,0,0,2,2,0,0,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,1
 data 1,0,0,2,2,2,2,0,2,0,0,0,0,0,1,0,1,0,1,0,1,0,0,0,0,0,0,0,0,0,0,1
 data 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,1
 data 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1,1,0,0,0,0,0,0,0,0,0,1
 data 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,1,1,0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0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1
 data 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1
 data 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1







Viewing Page [1] of [1]



Want More Source Codes?:



Release Type: The source code & tutorials found on this site are released as license ware for PlayBasic Users. No Person or Company may redistribute any file (tutorial / source code or media files) from this site, without explicit written permission.

 

 
     
 
       

(c) Copyright 2002 / 2024 Kevin Picone , UnderwareDesign.com  - Privacy Policy   Site: V0.99a [Alpha]